package com.enjia.common.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 对SharedPreference封装工具类
 * Created by 陈飞飞 on 2017/5/6.
 */

public class SharedPreferenceUtil {

    public static String SHARED_NAME="shared_date";

    private static SharedPreferences sharedPreferences;

    //存入int类型数据
    public static void saveIntData(Context context,String key,int value){
        if(sharedPreferences == null){
            sharedPreferences = context.getSharedPreferences(SHARED_NAME, Context.MODE_PRIVATE);
        }
        sharedPreferences.edit().putInt(key, value).commit();
    }
    //取出int类型数据
    public static int getIntData(Context context,String key,int defValue){
        if(sharedPreferences == null){
            sharedPreferences = context.getSharedPreferences(SHARED_NAME, Context.MODE_PRIVATE);
        }
        return sharedPreferences.getInt(key, defValue);
    }
    //存入String类型数据
    public static void saveStringData(Context context,String key,String value){
        if(sharedPreferences == null){
            sharedPreferences = context.getSharedPreferences(SHARED_NAME, Context.MODE_PRIVATE);
        }
        sharedPreferences.edit().putString(key, value).commit();
    }
    //取出String类型数据
    public static String getStringData(Context context,String key,String defValue){
        if(sharedPreferences == null){
            sharedPreferences = context.getSharedPreferences(SHARED_NAME, Context.MODE_PRIVATE);
        }
        return sharedPreferences.getString(key, defValue);
    }

}
